package deustotech.mobility.vc.display;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetAddress;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JToggleButton;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import deustotech.mobility.vc.core.Controller;


/**
 *
 * @author iigo
 */
public class MenuFrame extends javax.swing.JFrame implements ActionListener,ListSelectionListener {

	private javax.swing.JTextArea Console;
	private javax.swing.JList NodeList;
	private javax.swing.JToggleButton OnButton;
	private javax.swing.JPanel PanelBase;
	private javax.swing.JButton StartButton;
	private javax.swing.JButton StopButton;
	private javax.swing.JToolBar Toolbar;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JScrollPane jScrollPane2;
	public Vector<String> listData;
	private javax.swing.JTextField dir;
	private Controller c;


	public MenuFrame() {
		initComponents();
		c = Controller.getInstance();
	}



	private void initComponents() {

		PanelBase = new javax.swing.JPanel();
		Toolbar = new javax.swing.JToolBar();
		jScrollPane1 = new javax.swing.JScrollPane();
		Console = new javax.swing.JTextArea();
		jScrollPane2 = new javax.swing.JScrollPane();
		NodeList = new javax.swing.JList();
		StartButton = new javax.swing.JButton();
		OnButton = new javax.swing.JToggleButton();
		StopButton = new javax.swing.JButton();
		dir = new javax.swing.JTextField();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		getContentPane().setLayout(new java.awt.GridLayout(1, 0));

		Toolbar.setRollover(true);

		Console.setColumns(20);
		Console.setRows(5);
		jScrollPane1.setViewportView(Console);

		listData = new Vector<String>();
		listData.add(":: Nodes");
		NodeList = new javax.swing.JList(listData);

		jScrollPane2.setViewportView(NodeList);

		StartButton.setText("START");

		OnButton.setText("Send");

		StopButton.setText("STOP");

		dir.setText(" ");

		javax.swing.GroupLayout PanelBaseLayout = new javax.swing.GroupLayout(PanelBase);
		PanelBase.setLayout(PanelBaseLayout);
		PanelBaseLayout.setHorizontalGroup(
				PanelBaseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(Toolbar, javax.swing.GroupLayout.DEFAULT_SIZE, 557, Short.MAX_VALUE)
				.addGroup(PanelBaseLayout.createSequentialGroup()
						.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(18, 18, 18)
						.addGroup(PanelBaseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(dir, javax.swing.GroupLayout.PREFERRED_SIZE, 219, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE)
								.addGroup(PanelBaseLayout.createSequentialGroup()
										.addComponent(StartButton)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(StopButton)
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(OnButton)))
										.addGap(12, 12, 12))
				);
		PanelBaseLayout.setVerticalGroup(
				PanelBaseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(PanelBaseLayout.createSequentialGroup()
						.addComponent(Toolbar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(18, 18, 18)
						.addGroup(PanelBaseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
								.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
								.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE))
								.addGap(6, 6, 6)
								.addGroup(PanelBaseLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
										.addComponent(StartButton)
										.addComponent(StopButton)
										.addComponent(OnButton))
										.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(dir, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
										.addContainerGap())
				);

		getContentPane().add(PanelBase);
		OnButton.addActionListener(this);
		StartButton.addActionListener(this);
		StopButton.addActionListener(this);
		NodeList.addListSelectionListener(this);
		NodeList.clearSelection();
		dir.setText("No dir selected");
		setTitle("Timing Graphic User Interface");
		pack();
	}



	public void addNodes(Vector<InetAddress> inets) {
		System.out.println("Entrando en bucle");
		listData.clear();
		for(int i =0; i<inets.size();i++){
			listData.add(i,inets.get(i).getCanonicalHostName());
			System.out.println("Elemento: "+i);
		}
		NodeList.setListData(listData);

	}


	public void addAddress(InetAddress ineta) {
		//		listData.add(ineta.getCanonicalHostName());
		boolean found=false;
		for(int i =0; i<listData.size();i++)
			if(listData.get(i).equals(ineta.getCanonicalHostName()))
				found = true;
		if(found==false && ineta.getCanonicalHostName().startsWith("192.0."))
			listData.add(ineta.getCanonicalHostName());
		NodeList.setListData(listData);
	}


	public void post(String string) {

		Console.setText(Console.getText()+string+"\n");

	}


	public void valueChanged(ListSelectionEvent event) {
		if( event.getSource() == NodeList && !event.getValueIsAdjusting() && !OnButton.isSelected())
		{
			String stringValue = (String)NodeList.getSelectedValue();
			if( stringValue != null && !stringValue.startsWith("::"))
				dir.setText( stringValue );
		}
	}


	public void actionPerformed(ActionEvent evt) {
		Object inst = evt.getSource();
		if(inst instanceof JButton){
			System.out.println("equeals true");
			JButton jb = (JButton) inst;
			if(jb == this.StartButton){
				System.out.println("StartButton");

			}else if(jb == this.StopButton){
				System.out.println("StopButton");

			}

		}
		if(inst instanceof JToggleButton){
			JToggleButton jtb = (JToggleButton)inst;
			if(jtb.isSelected()){
				OnButton.setText("Sending");
				lanzarServidor();
			}if(!jtb.isSelected()){
				System.out.println("Parar servidor");
				//				System.out.println("Is OFF!");
				OnButton.setText("Send");
				pararServidor();
			}
		}


	}



	private void pararServidor() {

		c.pararUnicastServer(dir.getText());
	}


	private void lanzarServidor() {

		c.lanzarUnicastServer(dir.getText());

	}







}
